package Persistencia;

import Negocio.Automovel;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;



public class AutomovelDAO extends BaseDAO {

        private PreparedStatement stIncluir;
        private PreparedStatement stLista;
        private PreparedStatement stRemover;
        private PreparedStatement stConsulta;
        private PreparedStatement stTroca;

        public AutomovelDAO() throws IOException {
            super();
            Connection con = getConexao();
            try {
                stIncluir = con.prepareStatement("INSERT INTO automovel (modelo,cor,placa,anoFabri) VALUES (?,?,?,?)");
                stLista = con.prepareStatement("SELECT * FROM automovel order by codigo");
                stRemover = con.prepareStatement("DELETE * FROM automovel WHERE nomeC=?");
                stConsulta=con.prepareStatement("SELECT * FROM automovel WHERE nome ilike ? ");
                stTroca = con.prepareStatement("UPDATE automovel set nome=?,genero=?,classificacao=?,copias=? where codigo=?");
            } catch(SQLException e) {
                throw new IOException("Nao foi possivel criar as consultas: " + e.getMessage());
            }
        }

	public void incluir(Automovel f) throws IOException {
		try {

                    stIncluir.setString(1, f.getModelo());
                    stIncluir.setString(2, f.getCor());
                    stIncluir.setString(3, f.getPlaca());
                    stIncluir.setInt(4, f.AnoFabricacao());
                    stIncluir.executeUpdate();
                } catch(SQLException e) {
                    throw new IOException("Erro ao incluir: " + e.getMessage());
                }
	}

	public ArrayList<Automovel> consulta() throws IOException {
		ArrayList<Automovel> tmp = new ArrayList<Automovel>();
		try {
                        ResultSet rs = stLista.executeQuery();
                        while(rs.next()) {
                            tmp.add(new Automovel(rs.getInt("codigo"),rs.getString("modelo"),rs.getString("cor"),rs.getString("placa"),rs.getInt("anoFabri")));
                        }
		} catch(SQLException e) {
			throw new IOException("Erro ao consultar: " + e.getMessage());
		}
             return tmp;
	}
        public ArrayList<Automovel> consultaPorNome(String nome) throws IOException {
		ArrayList<Automovel> tmp = new ArrayList<Automovel>();
		try {
                        stConsulta.setString(1, nome + "%" );
                      //  System.out.println(stConsulta.)
                        ResultSet rs = stConsulta.executeQuery();
                        while(rs.next()) {
                            tmp.add(new Automovel(rs.getString("Modelo"),rs.getString("cor"),rs.getString("placa"),rs.getInt("anoFabri")));
                        }
		} catch(SQLException e) {
			throw new IOException("Erro ao consultar: " + e.getMessage());
		}
             return tmp;
	}
        public void trocar(Automovel f) throws IOException {
        try {

           stTroca.setString(1, f.getModelo());
           stTroca.setString(2, f.getCor());
           stTroca.setString(3, f.getPlaca());
           stTroca.setInt(4, f.AnoFabricacao());
           stTroca.setInt(5, f.getCodigo());

            stTroca.executeUpdate();
        } catch (SQLException e) {
            throw new IOException("Erro ao alterar : " + e.getMessage());
        }
    }
}
